﻿Imports System.Text
Imports System.Runtime.CompilerServices
Imports iConverter.Configuration




Namespace Core

    ''' <summary>
    ''' 编码管理器
    ''' </summary>
    ''' <remarks></remarks>
    Public Module EncodingManager

        ''' <summary>
        ''' 获取  UTF-7 格式的编码
        ''' </summary>
        Dim _utf7 As New UTF7Encoding()

        ''' <summary>
        ''' 获取  UTF-8 格式的编码
        ''' </summary>
        ''' <remarks></remarks>
        Dim _utf8 As New UTF8Encoding(True)



        ''' <summary>
        ''' 获取使用 Big-Endian 字节顺序的 UTF-16 格式的编码。
        ''' </summary>
        ''' <remarks></remarks>
        Dim _unicodeBE As New UnicodeEncoding(True, True)


        ''' <summary>
        ''' 获取使用 Little-Endian 字节顺序的 UTF-16 格式的编码。
        ''' </summary>
        ''' <remarks></remarks>
        Dim _unicodeLE As New UnicodeEncoding(False, True)


        ''' <summary>
        ''' 获取使用 Big-Endian 字节顺序的 UTF-32 格式的编码。
        ''' </summary>
        ''' <remarks></remarks>
        Dim _utf32BE As New UTF32Encoding(True, True)

        ''' <summary>
        ''' 获取使用 Little-Endian 字节顺序的 UTF-32 格式的编码。
        ''' </summary>
        ''' <remarks></remarks>
        Dim _utf32LE As New UTF32Encoding(False, True)


        ''' <summary>
        ''' 编码器列表
        ''' </summary>
        ''' <remarks></remarks>
        Private _UnicodeEncodingList As EncodingList



        ''' <summary>
        ''' 获取使用 Big-Endian 字节顺序的 UTF-32 格式的编码。
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property UTF32BigEndian() As UTF32Encoding
            Get
                Return _utf32BE
            End Get
        End Property

        ''' <summary>
        ''' 获取使用 Little-Endian 字节顺序的 UTF-32 格式的编码。
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property UTF32LittleEndian() As UTF32Encoding
            Get
                Return _utf32LE
            End Get
        End Property

        ''' <summary>
        ''' 获取  UTF-7 格式的编码
        ''' </summary>
        Public ReadOnly Property UTF7() As UTF7Encoding
            Get
                Return _utf7
            End Get
        End Property
        ''' <summary>
        ''' 获取  UTF-8 格式的编码
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property UTF8() As UTF8Encoding
            Get
                Return _utf8
            End Get
        End Property

        ''' <summary>
        ''' 获取使用 Big-Endian 字节顺序的 UTF-16 格式的编码。
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property UnicodeBigEndian() As UnicodeEncoding
            Get
                Return _unicodeBE
            End Get
        End Property


        ''' <summary>
        ''' 获取使用 Little-Endian 字节顺序的 UTF-16 格式的编码。
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property UnicodeLittleEndian() As UnicodeEncoding
            Get
                Return _unicodeLE
            End Get
        End Property




        ''' <summary>
        ''' Unicode 编码器列表
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property UnicodeEncodingList() As EncodingList
            Get
                If _UnicodeEncodingList Is Nothing Then
                    _UnicodeEncodingList = New EncodingList
                    _UnicodeEncodingList.Add(_utf8)
                    _UnicodeEncodingList.Add(_unicodeBE)
                    _UnicodeEncodingList.Add(_unicodeLE)
                    _UnicodeEncodingList.Add(_utf32BE)
                    _UnicodeEncodingList.Add(_utf32LE)

                End If

                Return _UnicodeEncodingList
            End Get
        End Property


        ''' <summary>
        ''' 系统默认编码
        ''' </summary>
        ''' <remarks></remarks>
        Dim _DefaultEncoding = Encoding.Default


        ''' <summary>
        ''' 获取当前系统默认编码
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property DefaultEncoding As Encoding
            Get
                Return _DefaultEncoding
            End Get
        End Property



        ''' <summary>
        ''' 源编码列表
        ''' </summary>
        ''' <remarks></remarks>
        Dim _SourceEncodingList As EncodingList

        ''' <summary>
        ''' 源编码列表
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property SourceEncodingList() As EncodingList
            Get
                If _SourceEncodingList Is Nothing Then
                    _SourceEncodingList = New EncodingList


                    _SourceEncodingList.Add(DefaultEncoding)
                    '从配置中初始化源编码列表
                    _SourceEncodingList.AddRange(SettingManager.Settings.EncodingList)
                    '_SourceEncodingList.Add(Encoding.GetEncoding(932)) '日文

                    _SourceEncodingList.AddRange(UnicodeEncodingList)
                End If
                Return _SourceEncodingList
            End Get
        End Property


        ''' <summary>
        ''' 目标编码列表
        ''' </summary>
        ''' <remarks></remarks>
        Dim _DestinationEncodingList As EncodingList

        ''' <summary>
        ''' 目标编码列表
        ''' </summary>
        ''' <remarks></remarks>
        Public ReadOnly Property DestinationEncodingList() As EncodingList
            Get
                If _DestinationEncodingList Is Nothing Then
                    _DestinationEncodingList = New EncodingList

                    _DestinationEncodingList.AddRange(UnicodeEncodingList)

                    '从配置中初始化目标编码列表
                    If SettingManager.Settings.isUseAdvancedDestinationEncodingList Then
                        _DestinationEncodingList.AddRange(SettingManager.Settings.EncodingList)
                    End If
                End If

                Return _DestinationEncodingList
            End Get
        End Property

        ''' <summary>
        ''' 清除原编码和目标编码缓存
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub ClearEncodingList()
            _SourceEncodingList = Nothing
            _DestinationEncodingList = Nothing

            RaiseEvent EncodingListChanged(Nothing, EventArgs.Empty)
        End Sub

        ''' <summary>
        ''' 编码列表更改
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Event EncodingListChanged(ByVal sender As Object, ByVal e As EventArgs)


        ''' <summary>
        ''' 所有编码
        ''' </summary>
        ''' <remarks></remarks>
        Private _AllEncodings As EncodingList

        ''' <summary>
        ''' 所有编码
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property AllEncodings() As EncodingList
            Get
                If _AllEncodings Is Nothing Then
                    Dim list = From ei In Encoding.GetEncodings()
                               Select Encoding.GetEncoding(ei.CodePage)

                    _AllEncodings = New EncodingList(list)
                End If
                Return _AllEncodings
            End Get
        End Property



#Region "扩展方法"

        ''' <summary>
        ''' 检查指定的编码是否是Unicode编码
        ''' </summary>
        ''' <param name="encoding">要检查的编码</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Extension()>
        Public Function IsUnicodeEncoding(ByVal encoding As Encoding) As Boolean
            Return UnicodeEncodingList.Contains(encoding)
        End Function


#End Region






    End Module


End Namespace

